package chat

import (
	"context"
	"encoding/json"
	"strconv"
	"strings"

	"mall/common/cachex"
	"mall/common/globalkey"
	"mall/service/backend/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/api/internal/logic/setting/util"
	"mall/service/setting/rpc/settingclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type (
	// Chat is an interface to be customized, add more methods here,
	// and implement the added methods in customChat.
	Chat interface {
		List(userId uint64) ([]*types.ChatInfo, error)
	}

	customChat struct {
		ctx    context.Context
		svcCtx *svc.ServiceContext
	}
)

func NewChat(ctx context.Context, svcCtx *svc.ServiceContext) Chat {
	return &customChat{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *customChat) List(userId uint64) (resp []*types.ChatInfo, err error) {
	memberRankKey := cachex.LocalMemberRank

	localCacheMemberRankList, ok := l.svcCtx.LocalCache.Get(memberRankKey)
	if ok {
		resp = localCacheMemberRankList.([]*types.ChatInfo)
	} else {
		resp, err = l.list(userId)
		if err != nil {
			return nil, err
		}

		l.svcCtx.LocalCache.Set(memberRankKey, resp)
	}

	return resp, nil
}
